package com.hj.collegetravel.controller;

import com.hj.collegetravel.models.*;
import com.hj.collegetravel.services.IAdminService;
import com.hj.collegetravel.services.IUserService;
import com.hj.collegetravel.tools.*;
import com.hj.collegetravel.tools.vcode.Captcha;
import com.hj.collegetravel.tools.vcode.GifCaptcha;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private IAdminService adminService;

    @RequestMapping("/{url}")
    public String url(@PathVariable String url) {
        System.out.println("+++++++++++++++++++通用URL++++++++++++++++++++++++++++");
        return "/admin/"+url.replace(".html","");
    }

    @RequestMapping("")
    public String index1(HttpServletRequest request) {
        return "redirect:index";
    }

    @RequestMapping("/")
    public String index0(HttpServletRequest request) {
        return "redirect:index";
    }

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        adminService.index(request);
        return "/admin/index";
    }

    /**
     * 登录
     */
    @GetMapping("/login")
    public String login(HttpServletRequest request){
        request.setAttribute("admin_login",CookieUtils.getCookie("admin_login"));
        return "/admin/login";
    }

    /**
     * 注销登录
     */
    @GetMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("admin");
        return "redirect:login" ;
    }

    @GetMapping("/users")
    public String users(HttpServletRequest request) {
        return "/admin/users";
    }

    @GetMapping("/userAdd")
    public String userAdd(HttpServletRequest request) {
        return "/admin/userAdd";
    }

    @GetMapping("/userEdit")
    public String userEdit(Integer uId,HttpServletRequest request) {
        adminService.userEdit(uId,request);
        return "/admin/userEdit";
    }

    @GetMapping("/schoolEdit")
    public String schoolEdit(Integer sId,HttpServletRequest request) {
        adminService.schoolEdit(sId,request);
        return "/admin/schoolEdit";
    }

    @GetMapping("/adsEdit")
    public String adsEdit(Integer iId,HttpServletRequest request) {
        adminService.adsEdit(iId,request);
        return "/admin/adsEdit";
    }

    @GetMapping("/views")
    public String views(HttpServletRequest request) {
        adminService.views(request);
        return "/admin/views";
    }

    @GetMapping("/viewAdd")
    public String viewAdd(HttpServletRequest request) {
        adminService.views(request);
        return "/admin/viewAdd";
    }

    @GetMapping("/viewEdit")
    public String viewEdit(Integer vId,HttpServletRequest request) {
        adminService.viewEdit(vId,request);
        return "/admin/viewEdit";
    }

    @GetMapping("/roleEdit")
    public String roleEdit(Integer aId,HttpServletRequest request) {
        adminService.roleEdit(aId,request);
        return "/admin/roleEdit";
    }

    @GetMapping("/comments")
    public String comments(HttpServletRequest request) {
        adminService.comments(request);
        return "/admin/comments";
    }

    @GetMapping("/pictures")
    public String pictures(Integer flag,Integer id,HttpServletRequest request) {
        adminService.pictures(flag,id,request);
        return "/admin/pictures";
    }

    @GetMapping("/pictureAdd")
    public String pictureAdd(Integer flag,Integer id,HttpServletRequest request) {
        adminService.pictures(flag,id,request);
        return "/admin/pictureAdd";
    }

    @GetMapping("/info")
    public String info(HttpServletRequest request) {
        adminService.index(request);
        return "/admin/info";
    }

    @GetMapping("/updateEdit")
    public String updateEdit(Integer uId,HttpServletRequest request) {
        adminService.updateEdit(uId,request);
        return "/admin/updateEdit";
    }

    /**
     * 获取验证码（Gif版本）
     */
    @RequestMapping("getGifCode")
    public void getGifCode(HttpServletResponse response,HttpServletRequest request){
        try {
            HttpSession session = request.getSession(true);
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/gif");
            /**
             * gif格式动画验证码
             * 宽，高，位数。
             */
            Captcha captcha = new GifCaptcha(120,40,4);
            //输出
            captcha.out(response.getOutputStream());
            //存入Session
            session.setAttribute("vcode",captcha.text().toUpperCase());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


//==================================================================================Layui json
//==================================================================================Layui json
//==================================================================================Layui json

    /**
     * 获取用户列表
     * @param id,username,email,sex 搜索
     * @param page 页号
     * @param limit 每页条数
     */
    @RequestMapping("/getUsers")
    @ResponseBody
    public ResultLayui getUsers(String id,String username,String email,Integer sex, @Param("page") int page, @Param("limit") int limit, HttpServletRequest request){
        List<CUser> list = adminService.getUsers(id,username,email,sex,request);
        int before = limit * (page - 1);
        int after = page * limit;
        if (after>list.size())
            after=list.size();
        int count = list.size();
        return ResultLayui.ok(count, list.subList(before,after));
    }

    /**
     * 获取学校列表
     */
    @RequestMapping("/getSchools")
    @ResponseBody
    public ResultLayui getSchools(String id,String name, @Param("page") int page, @Param("limit") int limit, HttpServletRequest request){
        List<CSchool> list = adminService.getSchools(id,name,request);
        int before = limit * (page - 1);
        int after = page * limit;
        if (after>list.size())
            after=list.size();
        int count = list.size();
        return ResultLayui.ok(count, list.subList(before,after));
    }

    /**
     * 获取首页推荐列表
     */
    @RequestMapping("/getAds")
    @ResponseBody
    public ResultLayui getAds(String id,String desc, @Param("page") int page, @Param("limit") int limit, HttpServletRequest request){
        List<CIndex> list = adminService.getAds(id,desc,request);
        int before = limit * (page - 1);
        int after = page * limit;
        if (after>list.size())
            after=list.size();
        int count = list.size();
        return ResultLayui.ok(count, list.subList(before,after));
    }

    /**
     * 获取景点列表
     */
    @RequestMapping("/getViews")
    @ResponseBody
    public ResultLayui getViews(String id,String name,Integer flag,Integer school, @Param("page") int page, @Param("limit") int limit, HttpServletRequest request){
        List<CView> list = adminService.getViews(id,name,flag,school,request);
        int before = limit * (page - 1);
        int after = page * limit;
        if (after>list.size())
            after=list.size();
        int count = list.size();
        return ResultLayui.ok(count, list.subList(before,after));
    }

    /**
     * 获取管理员列表
     */
    @RequestMapping("/getRoles")
    @ResponseBody
    public ResultLayui getRoles(String id,String username,String email,String phone, @Param("page") int page, @Param("limit") int limit, HttpServletRequest request){
        List<CAdmin> list = adminService.getRoles(id,username,email,phone,request);
        int before = limit * (page - 1);
        int after = page * limit;
        if (after>list.size())
            after=list.size();
        int count = list.size();
        return ResultLayui.ok(count, list.subList(before,after));
    }


    /**
     * 获取评论列表
     */
    @RequestMapping("/getComments")
    @ResponseBody
    public ResultLayui getComments(String id, Integer flag, Integer view, @Param("page") int page, @Param("limit") int limit, HttpServletRequest request){
        List<CComment> list = adminService.getComments(id,flag,view,request);
        int before = limit * (page - 1);
        int after = page * limit;
        if (after>list.size())
            after=list.size();
        int count = list.size();
        return ResultLayui.ok(count, list.subList(before,after));
    }

    /**
     * 图片管理
     */
    @RequestMapping("/getPictures")
    @ResponseBody
    public ResultLayui getPictures(Integer id, Integer flag, HttpServletRequest request){
        List<CPicture> list = adminService.getPictures(id,flag,request);
        return ResultLayui.ok(list);
    }
    /**
     * APP更新
     */
    @RequestMapping("/getUpdates")
    @ResponseBody
    public ResultLayui getUpdates(Integer id, Integer flag, HttpServletRequest request){
        List<CUpdate> list = adminService.getUpdates(id,flag,request);
        return ResultLayui.ok(list);
    }





//==================================================================================post
//==================================================================================post
//==================================================================================post

    /**
     * 登录
     * @param login 用户名
     * @param pwd 密码
     * @param code 验证码
     */
    @PostMapping("/dologin")
    public void dologin(String login,String pwd,String code, HttpServletRequest request, HttpServletResponse response) throws IOException {
        CheckCode checkCode = new CheckCode();
        if (true){//checkCode.checkVcode(code,request)) {
            boolean bl = adminService.dologin(login, pwd, request);
            if (bl) {
                response.getWriter().write("{\"status\":1,\"tips\":\"登录成功。\"}");
            } else
                response.getWriter().write("{\"status\":0,\"tips\":\"用户名或密码错误。\"}");
        }
        else
            response.getWriter().write("{\"Status\":-1,\"tips\":\"验证码不正确！\"}");
    }

    /**
     * 验证密码
     */
    @PostMapping("/checkpass")
    public void checkpass(String pass, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.checkpass(pass,request);
        if (bl) {
            response.getWriter().write("{\"status\":1,\"tips\":\"通过。\"}");
        } else
            response.getWriter().write("{\"status\":0,\"tips\":\"失败。\"}");
    }

    /**
     * 添加用户
     */
    @PostMapping("/addUser")
    public void addUser(CUser cUser, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.addUser(cUser,request);
        if (bl) {
            response.getWriter().write("{\"status\":1,\"tips\":\"添加成功。\"}");
        } else
            response.getWriter().write("{\"status\":0,\"tips\":\"添加失败。\"}");
    }

    /**
     * 编辑用户
     */
    @PostMapping("/editUser")
    public void editUser(CUser cUser, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.editUser(cUser,request);
        if (bl) {
            response.getWriter().write("{\"status\":1,\"tips\":\"编辑成功。\"}");
        } else
            response.getWriter().write("{\"status\":0,\"tips\":\"编辑失败。\"}");
    }

    /**
     * 删除用户
     */
    @PostMapping("/delUser")
    public void delUser(@RequestParam(value = "ids[]") Integer[] ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.delUser(ids,request);
        if (bl){
            response.getWriter().write("{\"status\":1,\"tips\":\"删除成功。\"}");
        }
        else
            response.getWriter().write("{\"status\":0,\"tips\":\"删除失败。\"}");

    }

    /**
     * 上传图片
     */
    @PostMapping("/uploadpic")
    public void cropper(@RequestParam("file") String file,HttpServletRequest request, HttpServletResponse response) throws Exception {
        boolean bl = adminService.uploadpic(file,request);
        if (bl){
            response.getWriter().write("{\"status\":1,\"tips\":\"上传成功。\",\"src\":\""+request.getAttribute("src")+"\"}");
        }
        else
            response.getWriter().write("{\"status\":0,\"tips\":\"上传失败。\"}");
    }

    /**
     * 更换学校图片
     */
    @PostMapping("/eSchoolPic")
    public void eSchoolPic(@RequestParam("file") String file,Integer id,HttpServletRequest request, HttpServletResponse response) throws Exception {
        boolean bl = adminService.eSchoolPic(file,id,request);
        if (bl){
            response.getWriter().write("{\"status\":1,\"tips\":\"更换成功。\",\"src\":\""+request.getAttribute("src")+"\"}");
        }
        else
            response.getWriter().write("{\"status\":0,\"tips\":\"更换失败。\"}");
    }

    /**
     * 添加学校
     */
    @PostMapping("/addSchool")
    public void addSchool(CSchool cSchool, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.addSchool(cSchool,request);
        if (bl) {
            response.getWriter().write("{\"status\":1,\"tips\":\"添加成功。\"}");
        } else
            response.getWriter().write("{\"status\":0,\"tips\":\"添加失败。\"}");
    }

    /**
     * 编辑学校
     */
    @PostMapping("/editSchool")
    public void editSchool(CSchool cSchool, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.editSchool(cSchool,request);
        if (bl) {
            response.getWriter().write("{\"status\":1,\"tips\":\"编辑成功。\"}");
        } else
            response.getWriter().write("{\"status\":0,\"tips\":\"编辑失败。\"}");
    }

    /**
     * 删除学校
     */
    @PostMapping("/delSchool")
    public void delSchool(@RequestParam(value = "ids[]") Integer[] ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.delSchool(ids,request);
        if (bl){
            response.getWriter().write("{\"status\":1,\"tips\":\"删除成功。\"}");
        }
        else
            response.getWriter().write("{\"status\":0,\"tips\":\"删除失败。\"}");

    }

    /**
     * 添加推荐
     */
    @PostMapping("/addAds")
    public void addAds(CIndex cIndex, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.addAds(cIndex,request);
        if (bl) {
            response.getWriter().write("{\"status\":1,\"tips\":\"添加成功。\"}");
        } else
            response.getWriter().write("{\"status\":0,\"tips\":\"添加失败。\"}");
    }

    /**
     * 更换推荐图片
     */
    @PostMapping("/eAdPic")
    public void eAdPic(@RequestParam("file") String file,Integer id,HttpServletRequest request, HttpServletResponse response) throws Exception {
        boolean bl = adminService.eAdPic(file,id,request);
        if (bl){
            response.getWriter().write("{\"status\":1,\"tips\":\"更换成功。\",\"src\":\""+request.getAttribute("src")+"\"}");
        }
        else
            response.getWriter().write("{\"status\":0,\"tips\":\"更换失败。\"}");
    }

    /**
     * 编辑推荐
     */
    @PostMapping("/editAds")
    public void editAds(CIndex cIndex, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.editAds(cIndex,request);
        if (bl) {
            response.getWriter().write("{\"status\":1,\"tips\":\"编辑成功。\"}");
        } else
            response.getWriter().write("{\"status\":0,\"tips\":\"编辑失败。\"}");
    }

    /**
     * 删除推荐
     */
    @PostMapping("/delAds")
    public void delAds(@RequestParam(value = "ids[]") Integer[] ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.delAds(ids,request);
        if (bl){
            response.getWriter().write("{\"status\":1,\"tips\":\"删除成功。\"}");
        }
        else
            response.getWriter().write("{\"status\":0,\"tips\":\"删除失败。\"}");

    }

    /**
     * 添加景点
     */
    @PostMapping("/addView")
    public void addView(CView cView, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.addView(cView,request);
        if (bl) {
            response.getWriter().write("{\"status\":1,\"tips\":\"添加成功。\"}");
        } else
            response.getWriter().write("{\"status\":0,\"tips\":\"添加失败。\"}");
    }

    /**
     * 编辑景点
     */
    @PostMapping("/editView")
    public void editView(CView cView, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.editView(cView,request);
        if (bl) {
            response.getWriter().write("{\"status\":1,\"tips\":\"编辑成功。\"}");
        } else
            response.getWriter().write("{\"status\":0,\"tips\":\"编辑失败。\"}");
    }

    /**
     * 删除景点
     */
    @PostMapping("/delView")
    public void delView(@RequestParam(value = "ids[]") Integer[] ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.delView(ids,request);
        if (bl){
            response.getWriter().write("{\"status\":1,\"tips\":\"删除成功。\"}");
        }
        else
            response.getWriter().write("{\"status\":0,\"tips\":\"删除失败。\"}");

    }

    /**
     * 添加管理员
     */
    @PostMapping("/addRole")
    public void addRole(CAdmin cAdmin, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.addRole(cAdmin,request);
        if (bl) {
            response.getWriter().write("{\"status\":1,\"tips\":\"添加成功。\"}");
        } else
            response.getWriter().write("{\"status\":0,\"tips\":\"添加失败。\"}");
    }

    /**
     * 编辑管理员
     */
    @PostMapping("/editRole")
    public void editRole(CAdmin cAdmin, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.editRole(cAdmin,request);
        if (bl) {
            response.getWriter().write("{\"status\":1,\"tips\":\"编辑成功。\"}");
        } else
            response.getWriter().write("{\"status\":0,\"tips\":\"编辑失败。\"}");
    }

    /**
     * 删除管理员
     */
    @PostMapping("/delRole")
    public void delRole(@RequestParam(value = "ids[]") Integer[] ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.delRole(ids,request);
        if (bl){
            response.getWriter().write("{\"status\":1,\"tips\":\"删除成功。\"}");
        }
        else
            response.getWriter().write("{\"status\":0,\"tips\":\"删除失败。\"}");

    }

    /**
     * 删除评价
     */
    @PostMapping("/delComment")
    public void delComment(@RequestParam(value = "ids[]") Integer[] ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.delComment(ids,request);
        if (bl){
            response.getWriter().write("{\"status\":1,\"tips\":\"删除成功。\"}");
        }
        else
            response.getWriter().write("{\"status\":0,\"tips\":\"删除失败。\"}");

    }

    /**
     * 添加图片
     */
    @PostMapping("/addPicture")
    public void addPicture(CPicture cPicture, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.addPicture(cPicture,request);
        if (bl) {
            response.getWriter().write("{\"status\":1,\"tips\":\"添加成功。\"}");
        } else
            response.getWriter().write("{\"status\":0,\"tips\":\"添加失败。\"}");
    }

    /**
     * 删除图片
     */
    @PostMapping("/delPicture")
    public void delPicture(@RequestParam(value = "ids[]") Integer[] ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.delPicture(ids,request);
        if (bl){
            response.getWriter().write("{\"status\":1,\"tips\":\"删除成功。\"}");
        }
        else
            response.getWriter().write("{\"status\":0,\"tips\":\"删除失败。\"}");

    }

    /**
     * 修改资料
     */
    @PostMapping("/setInfo")
    public void setInfo(CAdmin cAdmin, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.setInfo(cAdmin,request);
        if (bl) {
            response.getWriter().write("{\"status\":1,\"tips\":\"编辑成功。\"}");
        } else
            response.getWriter().write("{\"status\":0,\"tips\":\"编辑失败。\"}");
    }

    /**
     * 修改密码
     */
    @PostMapping("/setPassword")
    public void setPassword(String oldPassword,String password, HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer bl = adminService.setPassword(oldPassword,password,request);
        if (bl.equals(1)) {
            response.getWriter().write("{\"status\":1,\"tips\":\"修改成功。\"}");
        }
        else if (bl.equals(-1)){
            response.getWriter().write("{\"status\":-1,\"tips\":\"原密码不正确。\"}");
        }
        else
            response.getWriter().write("{\"status\":0,\"tips\":\"修改失败。\"}");
    }

    /**
     * 添加更新
     */
    @RequestMapping (value="/addUpdate") //@RequestParam(required=false)MultipartFile file 表示文件接受可为空
    public void addHomework (@RequestParam(required=false) MultipartFile file, CUpdate update,
                             HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer bl = adminService.addUpdate(file,update,request);
        if (bl.equals(1)){
            response.getWriter().write("{\"status\":1,\"tips\":\"发布成功。\"}");
        }
        else if (bl.equals(-1)) {
            response.getWriter().write("{\"status\":-1,\"tips\":\"请正确选择附件。\"}");
        }
        else if (bl.equals(-3)) {
            response.getWriter().write("{\"status\":-3,\"tips\":\"附件格式不支持。\"}");
        }
        else if (bl.equals(0)) {
            response.getWriter().write("{\"status\":0,\"tips\":\"发布失败。\"}");
        }
    }

    /**
     * 编辑更新
     */
    @PostMapping("/editUpdate")
    public void editUpdate(CUpdate update, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.editUpdate(update,request);
        if (bl) {
            response.getWriter().write("{\"status\":1,\"tips\":\"编辑成功。\"}");
        } else
            response.getWriter().write("{\"status\":0,\"tips\":\"编辑失败。\"}");
    }

    /**
     * 删除更新
     */
    @PostMapping("/delUpdate")
    public void delUpdate(@RequestParam(value = "ids[]") Integer[] ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean bl = adminService.delUpdate(ids,request);
        if (bl){
            response.getWriter().write("{\"status\":1,\"tips\":\"删除成功。\"}");
        }
        else
            response.getWriter().write("{\"status\":0,\"tips\":\"删除失败。\"}");

    }


}